<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="SPU" prop="prodSpu">
        <el-input
          v-model="queryParams.prodSpu"
          placeholder="请输入SPU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="prodKind">
        <el-select
          v-model="queryParams.prodKind"
          placeholder="请选择商品类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_prod_kind"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="prodName">
        <el-input
          v-model="queryParams.prodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品状态" prop="prodStatus">
        <el-select
          v-model="queryParams.prodStatus"
          placeholder="请选择商品状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_prod_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商" prop="appr">
        <el-input
          v-model="queryParams.appr"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:prod:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:prod:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:prod:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:prod:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="prodList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品ID" align="center" prop="prodId" />
      <el-table-column label="SPU" align="center" prop="prodSpu" />
      <el-table-column label="商品类型" align="center" prop="prodKind">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_prod_kind"
            :value="scope.row.prodKind"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品分类" align="center" prop="typeName" />
      <el-table-column label="商品名称" align="center" prop="prodName">
        <template slot-scope="scope">
          <span v-html="scope.row.prodName" />
        </template>
      </el-table-column>
      <el-table-column label="商品品牌" align="center" prop="brandName" />
      <el-table-column label="商品状态" align="center" prop="prodStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_prod_status"
            :value="scope.row.prodStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品单位" align="center" prop="unitId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_prod_unit"
            :value="scope.row.unitId"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品标签" align="center" prop="tagId">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_prod_tag"
            :value="scope.row.tagId"
          />
        </template>
      </el-table-column>
      <el-table-column label="供应商" align="center" prop="appr" />
      <el-table-column
        label="缩略图"
        align="center"
        prop="prodImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.prodImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:prod:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-folder"
            @click="handleAddAtta(scope.row)"
            v-hasPermi="['order:atta:add']"
            >附件</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleAddInfo(scope.row)"
            v-hasPermi="['order:info:add']"
            >信息</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:prod:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="SPU" prop="prodSpu">
          <el-input v-model="form.prodSpu" placeholder="请输入SPU" />
        </el-form-item>
        <el-form-item label="商品分类" prop="prodType">
          <el-cascader
            :options="typeList"
            :props="cascaderProps"
            :show-all-levels="false"
            v-model="form.prodType"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="商品品牌" prop="brandId">
          <el-select v-model="form.brandId" placeholder="请选择商品品牌">
            <el-option
              v-for="brand in brandList"
              :key="brand.brandId"
              :label="brand.brandName"
              :value="brand.brandId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型" prop="prodKind">
          <el-select v-model="form.prodKind" placeholder="请选择商品类型">
            <el-option
              v-for="dict in dict.type.sys_prod_kind"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品名称" prop="prodName">
          <el-input v-model="form.prodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品单位" prop="unitId">
          <el-select v-model="form.unitId" placeholder="请选择商品单位">
            <el-option
              v-for="dict in dict.type.sys_prod_unit"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品标签" prop="tagId">
          <el-select v-model="form.tagId" placeholder="请选择商品标签">
            <el-option
              v-for="dict in dict.type.sys_prod_tag"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="appr">
          <el-input v-model="form.appr" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="缩略图" prop="prodImage">
          <image-upload v-model="form.prodImage" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改商品附件对话框 -->
    <el-dialog
      :title="attaTitle"
      :visible.sync="attaOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="attaForm"
        :model="attaForm"
        :rules="attaRules"
        label-width="80px"
      >
        <el-form-item label="商品视频" prop="attaVideo">
          <file-upload v-model="attaForm.attaVideo" :fileType="['mp4']" />
        </el-form-item>
        <el-form-item label="上架标识" prop="upFlag">
          <el-radio-group v-model="attaForm.upFlag">
            <el-radio :label="0">立即上架</el-radio>
            <el-radio :label="1">定时上架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="定时上架时间"
          prop="upTime"
          v-if="attaForm.upFlag === 1"
        >
          <el-date-picker
            clearable
            v-model="attaForm.upTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择定时上架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下架标识" prop="downFlag">
          <el-radio-group v-model="attaForm.downFlag">
            <el-radio :label="0">立即下架</el-radio>
            <el-radio :label="1">定时下架</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="定时下架时间"
          prop="downTime"
          v-if="attaForm.downFlag === 1"
        >
          <el-date-picker
            clearable
            v-model="attaForm.downTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择定时下架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="轮播图片" prop="attaImages">
          <image-upload v-model="attaForm.attaImages" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAttaForm">确 定</el-button>
        <el-button @click="cancelAtta">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改商品信息对话框 -->
    <el-dialog
      :title="infoTitle"
      :visible.sync="infoOpen"
      width="500px"
      append-to-body
    >
      <el-form
        ref="infoForm"
        :model="infoForm"
        :rules="infoRules"
        label-width="80px"
      >
        <el-form-item label="SKU" prop="prodSku">
          <el-input v-model="infoForm.prodSku" placeholder="请输入SKU" />
        </el-form-item>
        <el-form-item label="商品名称" prop="prodName">
          <el-input v-model="infoForm.prodName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="售价" prop="prodPrice">
          <el-input v-model="infoForm.prodPrice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="成本价" prop="costPrice">
          <el-input v-model="infoForm.costPrice" placeholder="请输入成本价" />
        </el-form-item>
        <el-form-item label="库存" prop="prodStock">
          <el-input v-model="infoForm.prodStock" placeholder="请输入库存" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInfoForm">确 定</el-button>
        <el-button @click="cancelInfo">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProd,
  getProd,
  delProd,
  addProd,
  updateProd,
  listFromES,
} from "@/api/order/prod";
import { listBrand } from "@/api/order/brand";
import { listType } from "@/api/order/type";
import { addAtta } from "@/api/order/atta";
import { addInfo } from "@/api/order/info";
import scala from "highlight.js/lib/languages/scala";

export default {
  name: "Prod",
  dicts: ["sys_prod_unit", "sys_prod_kind", "sys_prod_tag", "sys_prod_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      prodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prodSpu: null,
        prodKind: null,
        prodType: null,
        prodName: null,
        brandId: null,
        prodStatus: null,
        appr: null,
        prodImage: null,
      },
      // 表单参数
      form: {
        prodType: null,
      },
      brandList: [],
      typeList: [],
      cascaderProps: {
        label: "typeName",
        value: "typeId",
        chidren: "children",
        checkStrictly: true,
      },
      // 表单校验
      rules: {
        prodName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        prodType: [
          { required: true, message: "请选择商品分类", trigger: "change" },
        ],
      },
      attaTitle: "",
      attaOpen: false,
      attaForm: {},
      attaRules: {},
      // 是否定时上架
      radio: "1",
      infoTitle: "",
      infoOpen: false,
      infoForm: {},
      infoRules: {},
    };
  },
  created() {
    this.getList();
    this.getBrandList();
    this.getTypeTree();
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      listFromES(this.queryParams).then((response) => {
        this.prodList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询品牌列表 */
    getBrandList() {
      let brandParam = {
        pageNum: 1,
        pageSize: 9999,
      };
      listBrand(brandParam).then((response) => {
        this.brandList = response.rows;
      });
    },
    /** 查询分类列表 */
    getTypeTree() {
      let typeParam = {
        pageNum: 1,
        pageSize: 9999,
      };
      listType(typeParam).then((response) => {
        this.typeList = this.handleTree(response.data, "typeId", "typePid");
        console.log("typeList", this.typeList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelAtta() {
      this.attaOpen = false;
      this.reset();
    },
    // 取消按钮
    cancelInfo() {
      this.infoOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        prodId: null,
        prodSpu: null,
        prodKind: null,
        prodType: null,
        prodName: null,
        brandId: null,
        prodStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        unitId: null,
        tagId: null,
        appr: null,
        prodImage: null,
      };
      this.resetForm("form");
      this.attaForm = {
        attaId: null,
        prodId: null,
        attaVideo: null,
        upFlag: null,
        upTime: null,
        downFlag: null,
        downTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        attaImages: null,
      };
      this.resetForm("attaForm");
      this.infoForm = {
        infoId: null,
        prodId: null,
        prodSku: null,
        prodName: null,
        prodPrice: null,
        costPrice: null,
        prodStock: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("infoForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.prodId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const prodId = row.prodId || this.ids;
      getProd(prodId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 添加商品附件操作 */
    handleAddAtta(row) {
      this.reset();
      const prodId = row.prodId || this.ids;
      this.attaForm.prodId = prodId;
      this.attaOpen = true;
      this.attaTitle = "添加商品附件";
    },
    /** 添加商品信息操作 */
    handleAddInfo(row) {
      this.reset();
      const prodId = row.prodId || this.ids;
      this.infoForm.prodId = prodId;
      this.infoOpen = true;
      this.infoTitle = "添加商品信息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (
            Array.isArray(this.form.prodType) &&
            this.form.prodType.length > 0
          ) {
            this.form.prodType =
              this.form.prodType[this.form.prodType.length - 1];
          } else if (!this.form.prodType) {
            this.form.prodType = null; // 或者设置为默认值
          }
          console.log("prodType", this.form.prodType);
          if (this.form.prodId != null) {
            updateProd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交附件按钮 */
    submitAttaForm() {
      this.$refs["attaForm"].validate((valid) => {
        if (valid) {
          addAtta(this.attaForm).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.attaOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 提交信息按钮 */
    submitInfoForm() {
      this.$refs["infoForm"].validate((valid) => {
        if (valid) {
          addInfo(this.infoForm).then((response) => {
            this.$modal.msgSuccess("新增成功");
            this.infoOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const prodIds = row.prodId || this.ids;
      this.$modal
        .confirm('是否确认删除商品编号为"' + prodIds + '"的数据项？')
        .then(function () {
          return delProd(prodIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "order/prod/export",
        {
          ...this.queryParams,
        },
        `prod_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
